package com.ym.medical.domain.bo.msg;

import cn.hutool.core.util.StrUtil;
import lombok.Data;

import java.util.List;

/**
 * @Author: DaiKuangYin
 * @Date: 2020/1/7 10:51
 */
@Data
public class MiniTemplate {

    private String touser;
    private String template_id;
    private String page;
    private List<TemplateParam> templateParamList;
    private String miniState;

    public MiniTemplate(String touser, String template_id, String page, List<TemplateParam> templateParamList) {
        this.touser = touser;
        this.template_id = template_id;
        this.page = page;
        this.templateParamList = templateParamList;
    }


    public MiniTemplate(String touser, String template_id, String page, List<TemplateParam> templateParamList, String miniState) {
        this.touser = touser;
        this.template_id = template_id;
        this.page = page;
        this.templateParamList = templateParamList;
        this.miniState = miniState;
    }

    public String toJSON() {
        StringBuffer buffer = new StringBuffer();
        buffer.append("{");
        buffer.append(String.format("\"touser\":\"%s\"", this.touser)).append(",");
        buffer.append(String.format("\"template_id\":\"%s\"", this.template_id)).append(",");
        buffer.append(String.format("\"page\":\"%s\"", this.page)).append(",");
        if(StrUtil.isNotBlank(this.miniState)){
            buffer.append(String.format("\"miniprogram_state\":\"%s\"", this.miniState)).append(",");
        }
        buffer.append("\"data\":{");
        TemplateParam param = null;
        for (int i = 0; i < this.templateParamList.size(); i++) {
            param = templateParamList.get(i);
            // 判断是否追加逗号
            if (i < this.templateParamList.size() - 1) {
                buffer.append(String.format("\"%s\": {\"value\":\"%s\"},", param.getKey(), param.getValue()));
            } else {
                buffer.append(String.format("\"%s\": {\"value\":\"%s\"}", param.getKey(), param.getValue()));
            }
        }
        buffer.append("}");
        buffer.append("}");
        return buffer.toString();
    }
}
